package com.hyou.springboot.util;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;

import com.hyou.springboot.consts.SessionPrefix;

@Component("sessionCacheUtil")
public class SessionCacheUtil {
    
    private SessionCacheUtil() {
    }
    
    private static SessionCacheUtil that;
    
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    
    @Value("${spring.session.timeout}")
    private int timeout = 300;
    
    @PostConstruct
    public void init() {
        SessionCacheUtil.that = this;
    }
    
    public static <T> void setAttribute(String attr, T value) {
        String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
        String redisKey = SessionPrefix.SESSION + sessionId;
        that.redisTemplate.boundHashOps(redisKey).put(attr, String.valueOf(value));
        that.redisTemplate.expire(redisKey, that.timeout, TimeUnit.SECONDS);
    }
    
    public static void setAttributes(Map<String, String> map) {
        String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
        String redisKey = SessionPrefix.SESSION + sessionId;
        that.redisTemplate.boundHashOps(redisKey).putAll(map);
        that.redisTemplate.expire(redisKey, that.timeout, TimeUnit.SECONDS);
    }
    
    public static String getAttribute(String attr) {
        String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
        String redisKey = SessionPrefix.SESSION + sessionId;
        String value = (String) that.redisTemplate.boundHashOps(redisKey).get(attr);
        that.redisTemplate.expire(redisKey, that.timeout, TimeUnit.SECONDS);
        return value;
    }
    
    public static void removeAttribute(Object... attr) {
        String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
        String redisKey = SessionPrefix.SESSION + sessionId;
        that.redisTemplate.boundHashOps(redisKey).delete(attr);
        that.redisTemplate.expire(redisKey, that.timeout, TimeUnit.SECONDS);
    }
    
    public static boolean hasAttribute(String attr) {
        String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
        String redisKey = SessionPrefix.SESSION + sessionId;
        boolean hasAttr = that.redisTemplate.boundHashOps(redisKey).hasKey(attr);
        that.redisTemplate.expire(redisKey, that.timeout, TimeUnit.SECONDS);
        return hasAttr;
    }
    
    public static boolean hasSession() {
        String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
        String redisKey = SessionPrefix.SESSION + sessionId;
        return that.redisTemplate.hasKey(redisKey);
    }
    
}
